// pages/feiyunyingzhe/feiyunyingzhe.js
import Toast from '@vant/weapp/toast/toast';
const { baseURL } = require("../../utils/request");
Page({
  data: {
    baseURL,
    introduce: "", //营地介绍
    value:'',
    show: false,
    showType: false,
    campLocation: "",
    fieldValue: '',
    campType: "",
    cascaderValue: '',
    columns: ['常规营地', '微型营地', '无人值守驻车点'],
  },
  onConfirm(event) {
    const { picker, value, index } = event.detail;
    Toast(`当前值：${value}, 当前索引：${index}`);
    this.setData({
      campType: value,
      showType: false
    })
  },
  onCancel() {
    Toast('取消');
  },
  //基本的输入框
  onChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
  },
  //级联输入框营地类型
  onClickType() {
    this.setData({
      showType: true,
    });
  },
  //营地类型设置内容
  onClose() {
    this.setData({
      show: false,
    });
  },
    // 绑定营地介绍
    changeIntroduce(e) {
      this.setData({
        introduce: e.detail.value
      })
    },
// 营地位置
selectSite() {
  // 检查用户权限状态
  wx.getSetting({
    success: res => {
      if (!res.authSetting['scope.userLocation']) {
        // 用户尚未授权或已拒绝授权
        // 主动请求授权
        this.requestLocationAuthorization();
      } else {
        // 已经授权，直接获取位置信息
        this.getUserLocation();
      }
    }
  })
},
//获取位置信息
getUserLocation() {
  wx.getLocation({
    // type: 'wgs84',
    type: 'gcj02',
    success: (res) => {
      console.log(res, "返回的数据")
      this.setData({
        latitude: res.latitude,
        longitude: res.longitude,
      });
      wx.chooseLocation({
        longitude: this.data.longitude,
        latitude: this.data.latitude,
        success: (res) => {
          console.log(res, '########');
          this.setData({
            latitude: res.latitude,
            longitude: res.longitude,
            campLocation: res.address
          });
        },
        fail: err => {
          console.log(err)
        }
      })
    },
    fail: (err) => {
      console.error('获取位置失败:', err);
    },
    complete: () => {
      console.log('位置获取操作已完成');
    }
  });
},
// 请求授权
requestLocationAuthorization() {
  // 请求用户授权精确位置
  wx.authorize({
    scope: 'scope.userLocation',
    success: () => {
      this.getUserLocation();
    },
    fail: (err) => {
      // 拒绝授权，提示用户收到开启
      this.showSettingPrompt()
    }
  })
},
showSettingPrompt() {
  wx.showModal({
    title: '需要位置权限',
    content: '请在设置中开启位置权限以正常使用本功能。',
    showCancel: false,
    confirmText: '去设置',
    success: (res) => {
      if (res.confirm) {
        wx.openSetting({
          success: (settingRes) => {
            if (settingRes.authSetting['scope.userLocation']) {
              // 用户在设置中开启了位置权限
              this.getUserLocation();
            } else {
              // 用户仍然拒绝位置权限
              console.log('用户仍未开启位置权限');
            }
          },
        });
      }
    },
  });
},
//图片上传
picture(){
  wx.chooseImage({
    success (res) {
      const tempFilePaths = res.tempFilePaths
      wx.uploadFile({
         url:'https://example.weixin.qq.com/upload', //仅为示例，非真实的接口地址
         filePath: tempFilePaths[0],
         name: 'file',
         formData: {
           'user': 'test'
         },
         success (res){
           const data = res.data
           //do something
         }
      })
    }
   })
},
    //获取图片
    getUploadList(l) {
      console.log(l)
      this.setData({
        campImgs: l.detail,
        tourisCampImgs: l.detail
      })
    },
        //删除图片
        delImgs(u) {
          this.data.campImgs.splice(u.currentTarget.dataset.index, 1)
          this.setData({
            campImgs: this.data.campImgs
          })
        },
})